package ex_from_web;


	import java.util.HashMap;
	import java.util.Iterator;
	import java.util.Map;


	public class mapping_1 {
	   /**
	    * How to iterate a Map in Java example
	    */
	   public static void main(String[] args) {
	    //Insted of HashMap, you can use 
	    //Hashtable, TreeMap or LinkedHashMap as well
	    Map<Integer, String> map = new HashMap<Integer, String>();
	    map.put(3, "Element 1");
	    map.put(2, "Element 2");
	    map.put(1, "Element 3");
	    
	    System.out.println("For loop 1"); //since Java 5
	    for (Map.Entry<Integer, String> entry : map.entrySet()) {
	        System.out.println("Key: " + entry.getKey() + " Value: " + entry.getValue());
	    }
	    
	    System.out.println("For loop 2"); //since Java 5
	    for (Integer key : map.keySet()) {
	        System.out.println("Key: " + key + " Value: " + map.get(key));
	    }
	    
	    System.out.println("Iterator");
	    Iterator<Integer> li = map.keySet().iterator();
	    while (li.hasNext()) {
	        Integer key = li.next();
	        System.out.println("Key: " + key + " Value: " + map.get(key));
	    }
	            
	    System.out.println("For loop /only values/"); //since Java 5
	    for (String value : map.values()) {
	        System.out.println("Value: " + value);
	    }
	    
	    System.out.println("Iterator /only values/");
	    Iterator<String> l = map.values().iterator();
	    while (l.hasNext()) {
	        String value = l.next();
	        System.out.println("Value: " + value);
	   }
	   }
}